using MediatR;
using RBAC.ErrorCode;
using RBAC.Write.Api.Application.Command.ProductionPlans;
using RBAC.Domain.ProductionPlans;
using RBAC.Interstructrue.Base;
using System;

namespace RBAC.Write.Api.Application.Handler.ProductionPlans
{
    public class UpdateProductionPlanCommandHandler : IRequestHandler<UpdateProductionPlanCommands, APIResult<int>>
    {
        private readonly IBaseRepository<ProductionPlan> _planRepo;

        public UpdateProductionPlanCommandHandler(IBaseRepository<ProductionPlan> planRepo)
        {
            _planRepo = planRepo;
        }

        public Task<APIResult<int>> Handle(UpdateProductionPlanCommands request, CancellationToken cancellationToken)
        {
            try
            {
                if (request == null || request.Id <= 0)
                {
                    return Task.FromResult(new APIResult<int> { Code = APIEnums.Fail, Msg = "参数错误：Id 无效", Data = 0 });
                }

                if (string.IsNullOrWhiteSpace(request.PlanTopic))
                    return Task.FromResult(new APIResult<int> { Code = APIEnums.Fail, Msg = "计划主题不能为空", Data = 0 });
                if (string.IsNullOrWhiteSpace(request.PlanCode))
                    return Task.FromResult(new APIResult<int> { Code = APIEnums.Fail, Msg = "计划单号不能为空", Data = 0 });

                // 存在性校验
                var model = _planRepo.GetModel(request.Id);
                if (model == null)
                {
                    return Task.FromResult(new APIResult<int> { Code = APIEnums.Fail, Msg = "记录不存在或已删除", Data = 0 });
                }

                // 唯一性校验：PlanCode 不允许与其它记录重复
                var dup = _planRepo.GetFirstOrDefault("PlanCode = @PlanCode AND Id <> @Id AND IsDeleted = 0", new { request.PlanCode, request.Id });
                if (dup != null)
                {
                    return Task.FromResult(new APIResult<int> { Code = APIEnums.Fail, Msg = "计划单号已存在", Data = 0 });
                }

                // 赋值更新字段
                model.PlanTopic = request.PlanTopic;
                model.PlanCode = request.PlanCode;
                model.DocumentsTime = request.DocumentsTime;
                model.AssociatedProjects = request.AssociatedProjects ?? string.Empty;
                model.Remark = request.Remark ?? string.Empty;
                model.GoodsId = request.GoodsId;
                model.SaleId = request.SaleId;

                var affected = _planRepo.Update(model);

                return Task.FromResult(new APIResult<int>
                {
                    Code = affected > 0 ? APIEnums.Success : APIEnums.Fail,
                    Msg = affected > 0 ? "修改成功" : "修改失败",
                    Data = affected
                });
            }
            catch (Exception ex)
            {
                return Task.FromResult(new APIResult<int>
                {
                    Code = APIEnums.Fail,
                    Msg = "修改异常: " + ex.Message,
                    Data = 0
                });
            }
        }
    }
}
